package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.PackingService;
import cn.itcast.service.cargo.ShippingService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/shipping")
public class ShippingController extends BaseController{

    @Reference
    private ShippingService shippingService;

    @Reference
    private PackingService packingService;


    //进行分页查询
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5") int size){
        ShippingExample example = new ShippingExample();
        ShippingExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        PageInfo info = shippingService.findAll(example, page, size);

        request.setAttribute("page",info);
        return "/cargo/shipping/shipping-list";
    }


    //跳转添加页面   查询装箱列表
    @RequestMapping("/toAdd")
    public String toAdd(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5") int size){
        PackingExample example = new PackingExample();
        PackingExample.Criteria criteria = example.createCriteria();
        criteria.andCompanyIdEqualTo(getLoginCompanyId());
        criteria.andStateEqualTo(1L);
        PageInfo info = packingService.findAll(example, page, size);
        request.setAttribute("page",info);
        return "cargo/shipping/shipping-add";
    }

    //跳转更新页面 数据回显
    @RequestMapping("toUpdate")
    public String toUpdate(String id){
        Shipping shipping = shippingService.findById(id);
        request.setAttribute("shipping",shipping);
        return "/cargo/shipping/shipping-update";
    }


    //保存 或 修改
    @RequestMapping("/edit")
    public String edit(String packingId,Shipping shipping){
        shipping.setCompanyId(getLoginCompanyId());
        shipping.setCompanyName(getLoginCompanyName());
        if (StringUtils.isEmpty(shipping.getId())){
            shipping.setId(packingId);
            shippingService.save(shipping);
        }else {
            shippingService.update(shipping);
        }
        return "redirect:/cargo/shipping/list.do";
    }



    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        shippingService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }
    //提交
    @RequestMapping("/submit")
    public String submit(String id){
        Shipping shipping = new Shipping();
        shipping.setId(id);
        shipping.setState(2);
        shippingService.update(shipping);
        return "redirect:/cargo/shipping/list.do";
    }
    //取消
    @RequestMapping("/cancel")
    public String cancel(String id){
        Shipping shipping = new Shipping();
        shipping.setId(id);
        shipping.setState(0);
        shippingService.update(shipping);
        return "redirect:/cargo/shipping/list.do";
    }

}
